package com.kcyu.egoodsmapbackend.entity;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import org.springframework.cglib.beans.BeanMap;

import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class WebPage<T> implements IPage<T> {

    private static final long serialVersionUID = -3946081876283517202L;
    private List<T> records;
    private long total;
    private long size;
    private long current;
    private String[] ascs;
    private String[] descs;
    private T queryCondition;
    public WebPage() {
        this.records = Collections.emptyList();
        this.total = 0L;
        this.size = 10L;
        this.current = 1L;
    }

    public WebPage(long current, long size) {
        this(current, size, 0L);
    }

    public WebPage(long current, long size, long total) {
        this.records = Collections.emptyList();
        this.total = 0L;
        this.size = 10L;
        this.current = 1L;
        if (current > 1L) {
            this.current = current;
        }

        this.size = size;
        this.total = total;
    }

    public boolean hasPrevious() {
        return this.current > 1L;
    }

    public boolean hasNext() {
        return this.current < this.getPages();
    }

    public List<T> getRecords() {
        return this.records;
    }

    public WebPage<T> setRecords(List<T> records) {
        this.records = records;
        return this;
    }

    public long getTotal() {
        return this.total;
    }

    public WebPage<T> setTotal(Long total) {
        this.total = total;
        return this;
    }

    public WebPage<T> setTotal(long total) {
        this.total = total;
        return this;
    }

    public long getSize() {
        return this.size;
    }

    public WebPage<T> setSize(long size) {
        this.size = size;
        return this;
    }

    public long getCurrent() {
        return this.current;
    }

    public WebPage<T> setCurrent(long current) {
        this.current = current;
        return this;
    }

    public String[] ascs() {
        return this.ascs;
    }

    public WebPage<T> setAsc(String... ascs) {
        this.ascs = ascs;
        return this;
    }

    public String[] descs() {
        return this.descs;
    }

    public WebPage<T> setDesc(String... descs) {
        this.descs = descs;
        return this;
    }

    public Map<Object, Object> condition() {
        return this.queryCondition == null ? null : new HashMap(beanToMap(this.queryCondition));
    }

    private static Map<String, Object> beanToMap(Object bean) {
        return null == bean ? null : BeanMap.create(bean);
    }

    public void setQueryCondition(T queryCondition) {
        this.queryCondition = queryCondition;
    }

    public T getQueryCondition() {
        return this.queryCondition;
    }

    public List<OrderItem> orders() {
        List<OrderItem> orders = new ArrayList();
        String[] var2;
        int var3;
        int var4;
        String desc;
        OrderItem o;
        if (this.ascs != null) {
            var2 = this.ascs;
            var3 = var2.length;

            for(var4 = 0; var4 < var3; ++var4) {
                desc = var2[var4];
                o = new OrderItem();
                o.setAsc(true);
                o.setColumn(this.filter(desc));
                orders.add(o);
            }
        }

        if (this.descs != null) {
            var2 = this.descs;
            var3 = var2.length;

            for(var4 = 0; var4 < var3; ++var4) {
                desc = var2[var4];
                o = new OrderItem();
                o.setAsc(false);
                o.setColumn(this.filter(desc));
                orders.add(o);
            }
        }

        return orders;
    }

    private String filter(String source) {
        Pattern pattern = Pattern.compile("\\s*|\t|\r|\n");
        Matcher matcher = pattern.matcher(source);
        String preStr = matcher.replaceAll("");
        String[] specialChar = new String[]{"*", "'", ";", "-", "--", "+", "//", "/", "%", "#", "$", "=", "\\", ",", "0x"};
        String[] var6 = specialChar;
        int var7 = specialChar.length;

        for(int var8 = 0; var8 < var7; ++var8) {
            String schar = var6[var8];
            if (preStr.contains(schar)) {
                return "-";
            }
        }

        return preStr;
    }
}
